import axios from 'axios'
export const  mixin = {
  methods: {
    // 获取歌单列表
    getSongLists () {
      let _this = this
      // 发送请求，获取歌单列表的分页结果
      const params = _this.$qs.stringify({
        page: 1,
        rows: 20,
        sortBy: 'create_time',
        desc: true
      })
      axios.get(_this.$store.state.HOST + '/songlist/page',params)
        .then(function (response) {
          console.log("歌单返回来的列表")
          console.log(response.data)
          _this.$store.commit('setSongsList', response.data.items)
          window.sessionStorage.setItem('songsList', JSON.stringify(response.data.items))

          _this.$store.commit('setTotal',response.data.total)
          window.sessionStorage.setItem('total',JSON.stringify(response.data.total))
          _this.$store.commit('setTotalPage',response.data.totalPage)
          window.sessionStorage.setItem('totalPage',JSON.stringify(response.data.totalPage))
        })
        .catch(function (error) {
          console.log(error)
        })
    },
    // 获取歌手
    getSingerLists () {
      let _this = this
      // 发送请求，获取歌手列表的分页结果
      const params = _this.$qs.stringify({
        page: 1,
        rows: 20,
        sortBy: 'create_time',
        desc: true
      })
      axios.get(_this.$store.state.HOST + '/singer/page',params)
        .then(function (response) {
          _this.$store.commit('setSingersList', response.data.items)
          window.sessionStorage.setItem('singersList', JSON.stringify(response.data.items))
          _this.$store.commit('setTotal',response.data.total)
          window.sessionStorage.setItem('total',JSON.stringify(response.data.total))
          _this.$store.commit('setTotalPage',response.data.totalPage)
          window.sessionStorage.setItem('totalPage',JSON.stringify(response.data.totalPage))
        })
        .catch(function (error) {
          console.log(error)
        })
    },
    // 播放
    toplay: function (id, url, pic, index, name,artist, lyric) {

      console.log("歌词：")
      console.log(id)
      console.log(url)
      console.log(pic)
      console.log(index)
      console.log(name)
      console.log(artist)
      this.$store.commit('setId', id)
      window.sessionStorage.setItem('id', JSON.stringify(id))
      this.$store.commit('setListIndex', index)
      window.sessionStorage.setItem('listIndex', JSON.stringify(index))
      this.$store.commit('setUrl', url)
      window.sessionStorage.setItem('url', JSON.stringify(url))
      this.$store.commit('setpicUrl', pic)
      window.sessionStorage.setItem('picUrl', JSON.stringify(pic))
      this.$store.commit('setTitle', name)
      window.sessionStorage.setItem('title', JSON.stringify(name))
      this.$store.commit('setArtist', artist)
      window.sessionStorage.setItem('artist', JSON.stringify(artist))
      this.$store.commit('setLyric', this.parseLyric(lyric))
      window.sessionStorage.setItem('lyric', JSON.stringify(this.parseLyric(lyric)))
      // console.log(this.$store.state.playing['songsList'])
      this.$router.push({path: '/player-page/' + id})
    },
    // 解析歌词
    parseLyric (text) {
      console.log(typeof text)
      var lines = text.split('\n'),
        pattern = /\[\d{2}:\d{2}.(\d{3}|\d{2})\]/g,
        result = []
      // console.log(lines)
      console.log("正则表达式解析后的歌词")
      while (!pattern.test(lines[0])) {
        lines = lines.slice(1)
        console.log(lines)
      };
      // console.log(lines.length)
      lines[lines.length - 1].length === 0 && lines.pop()
      console.log("-------歌词-------")
      for (let item of lines) {
        // console.log(item)
        let time = item.match(pattern) // 存前面的时间段
        let value = item.replace(pattern, '') // 存歌词
        // console.log("时间：" + time) // 时间
        // console.log("歌词：" + value) // 歌词数据
        for (let item1 of time) {
          var t = item1.slice(1, -1).split(':')
          if (value !== '') {
            result.push([parseInt(t[0], 10) * 60 + parseFloat(t[1]), value])
          }
        }
      }
      result.sort(function (a, b) {
        return a[0] - b[0]
      })
      console.log(result)
      return result
    },
    // 搜索音乐(没用到)
    getSong () {
      if (!this.$route.query.keywords) {
        this.$store.commit('setListOfSongs', [])
        this.$notify({
          title: '您输入内容为空',
          type: 'warning'
        })
      } else {
        let _this = this
        axios.get(_this.$store.state.HOST + '/listSongsOfSearch?name=' + _this.$route.query.keywords)
          .then(function (response) {
            // console.log(response.data)
            if (!response.data.length) {
              _this.$store.commit('setListOfSongs', [])
              _this.$notify({
                title: '系统暂无该歌曲',
                type: 'warning'
              })
            } else {
              _this.$store.commit('setListOfSongs', response.data)
              // window.sessionStorage.setItem('listOfSongs', JSON.stringify(response.data))
            }
          })
          .catch(function (error) {
            console.log(error)
          })
      }
    },
    // 认证
    goVerify () {
      axios.post(this.$store.state.HOST + "/verify").then(resp=>{
        console.log(resp.data)
        this.copyMsg(resp.data)
      }).catch(()=>{
        let user = {
          id: '',
          username: '',
          avator: '',
          sex: 1,
          mySignature:'',
          phone:'',
          location: '',
          createTime:''
        }
        this.copyMsg(user)
      })
    },
    logout () {
      axios.post(this.$store.state.HOST + "/logout").then(()=>{
        // 置空
        let user = {
          id: '',
          username: '',
          avator: '',
          sex: 1,
          mySignature:'',
          phone:'',
          location: '',
          createTime:''
        }
        this.copyMsg(user)
        this.$store.commit('setLoginIn', false)
        window.sessionStorage.setItem('loginIn', JSON.stringify(false))
        this.$message.success("退出成功")
        this.$router.push({path: '/home-page'})
      })
    },
    copyMsg (item) {
      console.log("测试为什么报错")
      this.$store.commit('setUserId', item.id)
      window.localStorage.setItem('userId', JSON.stringify(item.id))
      this.$store.commit('setUsername', item.username)
      window.localStorage.setItem('username', JSON.stringify(item.username))
      this.$store.commit('setAvator', item.avator)
      window.localStorage.setItem('avator', JSON.stringify(item.avator))
      this.$store.commit('setSex', item.sex)
      window.localStorage.setItem('sex', JSON.stringify(item.sex))
      this.$store.commit('setMySignature', item.mySignature)
      window.localStorage.setItem('mySignature', JSON.stringify(item.mySignature))
      this.$store.commit('setCreateTime', item.createTime)
      window.localStorage.setItem('createTime', JSON.stringify(item.createTime))
      this.$store.commit('setPhone', item.phone)
      window.localStorage.setItem('phone', JSON.stringify(item.phone))
      this.$store.commit('setLocation', item.location)
      window.localStorage.setItem('location', JSON.stringify(item.location))
    },
    getuploadSongLists() {
      axios.get(this.$store.state.HOST+ "/user//upload/list?userId="+this.userId).then(resp=>{
        this.uploadSongLists = resp.data
      }).catch(()=>{
        this.uploadSongLists = []
      })
    }
  }
}
